<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><div id="myscoll"></div><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>小游戏 | 晚夜微雨问海棠のBlog</title><meta name="keywords" content="游戏"><meta name="author" content="晚夜微雨问海棠"><meta name="copyright" content="晚夜微雨问海棠"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="游戏贪吃蛇游戏 贪吃蛇游戏是中介者模式开发的。我们这个贪吃蛇案例不是像之前一样都写到一个HTML文件中，而是进行了类的拆分，单独用js表示每个类，中介类是Game类，所以我们先引入这个类  初始Game类结构 index.html  123456789101112131415161718&lt;!DOCTYPE html&gt;&lt;html lang&#x3D;&quot;en&quot;&gt;&amp;lt">
<meta property="og:type" content="article">
<meta property="og:title" content="小游戏">
<meta property="og:url" content="http://example.com/posts/255e6169.html">
<meta property="og:site_name" content="晚夜微雨问海棠のBlog">
<meta property="og:description" content="游戏贪吃蛇游戏 贪吃蛇游戏是中介者模式开发的。我们这个贪吃蛇案例不是像之前一样都写到一个HTML文件中，而是进行了类的拆分，单独用js表示每个类，中介类是Game类，所以我们先引入这个类  初始Game类结构 index.html  123456789101112131415161718&lt;!DOCTYPE html&gt;&lt;html lang&#x3D;&quot;en&quot;&gt;&amp;lt">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s1.ax1x.com/2022/11/06/xXrJCF.jpg">
<meta property="article:published_time" content="2022-11-06T02:09:50.000Z">
<meta property="article:modified_time" content="2022-11-12T03:30:45.677Z">
<meta property="article:author" content="晚夜微雨问海棠">
<meta property="article:tag" content="游戏">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s1.ax1x.com/2022/11/06/xXrJCF.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/posts/255e6169"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="manifest" href="/manifest.json"/><meta name="msapplication-TileColor" content="#49b1f5"/><link rel="apple-touch-icon" sizes="180x180" href="/img/siteicon/128.png"/><link rel="icon" type="image/png" sizes="32x32" href="/img/siteicon/32.png"/><link rel="icon" type="image/png" sizes="16x16" href="/img/siteicon/16.png"/><link rel="mask-icon" href="/img/siteicon/128.png" color="#5bbad5"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":150},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":650,"languages":{"author":"作者: 晚夜微雨问海棠","link":"链接: ","source":"来源: 晚夜微雨问海棠のBlog","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"top-right"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '小游戏',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-12 11:30:45'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'"> <link rel="stylesheet" href="/swiper/swiper.min.css"> <link rel="stylesheet" href="/swiper/swiperstyle.css"> <link rel="stylesheet" href="/css/rightMenu.css"> <link rel="stylesheet" href="/css/constrain.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-categories-card@1.0.0/lib/categorybar.css"><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.css" /><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-double-row-display@1.00/cardlistpost.min.css"/>
<style>#recent-posts > .recent-post-item >.recent-post-info > .article-meta-wrap > .tags:before {content:"\A";
  white-space: pre;}#recent-posts > .recent-post-item >.recent-post-info > .article-meta-wrap > .tags > .article-meta__separator{display:none}</style>
<link rel="stylesheet" href="https://cdn.cbd.int/anzhiyu-blog@2.0.3/css/runtime/runtime.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-swiper-anzhiyu@1.0.4/lib/swiper.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-swiper-anzhiyu@1.0.4/lib/swiperstyle.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://cdn.cbd.int/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/atom.xml" title="晚夜微雨问海棠のBlog" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg" style="background:url(https://s1.ax1x.com/2022/11/06/xXrJCF.jpg);background-attachment: local;background-position: center;background-size: cover;background-repeat: no-repeat;"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://img2.woyaogexing.com/2022/06/28/de1ce3dc65468fd4!400x400.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">97</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">31</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">17</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/bangumis/index.html"><i class="fa-fw fas fa-home"></i><span> 追番</span></a></div><div class="menus_item"><a class="site-page" href="/comments/"><i class="fa-fw fas fa-envelope"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活馆</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://81.69.189.243:8082/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s1.ax1x.com/2022/11/06/xXrJCF.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">晚夜微雨问海棠のBlog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/bangumis/index.html"><i class="fa-fw fas fa-home"></i><span> 追番</span></a></div><div class="menus_item"><a class="site-page" href="/comments/"><i class="fa-fw fas fa-envelope"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活馆</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://81.69.189.243:8082/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">小游戏</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-11-06T02:09:50.000Z" title="发表于 2022-11-06 10:09:50">2022-11-06</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-12T03:30:45.677Z" title="更新于 2022-11-12 11:30:45">2022-11-12</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">5.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>25分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="小游戏"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="游戏"><a href="#游戏" class="headerlink" title="游戏"></a>游戏</h1><h2 id="贪吃蛇游戏"><a href="#贪吃蛇游戏" class="headerlink" title="贪吃蛇游戏"></a>贪吃蛇游戏</h2><ol>
<li>贪吃蛇游戏是中介者模式开发的。我们这个贪吃蛇案例不是像之前一样都写到一个HTML文件中，而是进行了类的拆分，单独用js表示每个类，中介类是Game类，所以我们先引入这个类</li>
</ol>
<h3 id="初始Game类结构"><a href="#初始Game类结构" class="headerlink" title="初始Game类结构"></a>初始Game类结构</h3><ol>
<li>index.html</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;!<span class="variable constant_">DOCTYPE</span> html&gt;</span><br><span class="line">&lt;html lang=&quot;en&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;</span><br><span class="line">    &lt;title&gt;Document&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div id=&quot;app&quot;&gt;&lt;/div&gt;</span><br><span class="line">    &lt;script src=&quot;js/Game.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">    &lt;script&gt;</span><br><span class="line">        new Game()</span><br><span class="line">    &lt;/script&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<ol>
<li>Game.js</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Game</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="title function_">alert</span>(<span class="string">&quot;我是game&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol>
<li><p>第二步我们要进行页面的初始化，初始化布局不是直接写html标签，而是通过game进行的初始化节点上树</p>
<p>我们设置的是20行20列的表格，用this.row表示行,this.col表示列</p>
</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Game</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">init</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">dom</span> = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;table&quot;</span>);</span><br><span class="line">    <span class="keyword">var</span> tr, td;</span><br><span class="line">    <span class="comment">// 遍历行和列上树</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="variable language_">this</span>.<span class="property">row</span>; i++) &#123;</span><br><span class="line">        <span class="comment">// 遍历行，创建节点上树</span></span><br><span class="line">        tr = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;tr&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="variable language_">this</span>.<span class="property">col</span>; j++) &#123;</span><br><span class="line">            <span class="comment">// 遍历列，创建节点上树</span></span><br><span class="line">            td = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;td&quot;</span>);</span><br><span class="line">            <span class="comment">// 追加到tr</span></span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(tr)</span><br><span class="line">            tr.<span class="title function_">appendChild</span>(td)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 追加节点上树</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">dom</span>.<span class="title function_">appendChild</span>(tr);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 表格上树</span></span><br><span class="line">    <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;app&quot;</span>).<span class="title function_">appendChild</span>(<span class="variable language_">this</span>.<span class="property">dom</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="渲染蛇的颜色"><a href="#渲染蛇的颜色" class="headerlink" title="渲染蛇的颜色"></a>渲染蛇的颜色</h3><ol>
<li>接下来就是渲染蛇的逻辑，此时我们的逻辑是Snake类调Game类的setColor方法，因为本质其实就是table表格渲染颜色，table表格在Game类初始创建的，所以理应让Game设置渲染颜色的方法</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 设置颜色的方法</span></span><br><span class="line"><span class="title class_">Game</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">setColor</span> = <span class="keyword">function</span>(<span class="params">row, col, color</span>) &#123;</span><br><span class="line">    <span class="comment">// 让表格的第几行，第几列设置什么颜色</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">dom</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;tr&quot;</span>)[row].<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;td&quot;</span>)[col].<span class="property">style</span>.<span class="property">background</span> = color</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol>
<li>此时我们让Snake类调用game的setColor方法</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Snake</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">render</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 蛇头的渲染</span></span><br><span class="line">    game.<span class="title function_">setColor</span>(<span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span>, <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span>, <span class="string">&#x27;pink&#x27;</span>);</span><br><span class="line">    <span class="comment">// 蛇的身体</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">1</span>; i &lt; <span class="variable language_">this</span>.<span class="property">body</span>.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        game.<span class="title function_">setColor</span>(<span class="variable language_">this</span>.<span class="property">body</span>[i].<span class="property">row</span>, <span class="variable language_">this</span>.<span class="property">body</span>[i].<span class="property">col</span>, <span class="string">&#x27;cyan&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol>
<li>此时有一个问题，Snake 的render谁来调用？如果我们Snake自己的构造函数中调用得不到game，因为此时的Game类还没有执行完四步走，所以现在是undefined的。</li>
<li>解决方案就是使用定时器去调用，因为定时器是异步的，所以不会阻止Game类的四步走</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> timer = <span class="built_in">setInterval</span>(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 定时器里面的核心就是游戏的渲染本质，清屏-更新-渲染</span></span><br><span class="line">    <span class="comment">// 蛇的渲染</span></span><br><span class="line">    game.<span class="property">snake</span>.<span class="title function_">render</span>();</span><br><span class="line"></span><br><span class="line">&#125;, <span class="number">1000</span>)</span><br></pre></td></tr></table></figure>
<ol>
<li>此时的table中就能够渲染蛇了</li>
</ol>
<h3 id="蛇的运动"><a href="#蛇的运动" class="headerlink" title="蛇的运动"></a>蛇的运动</h3><ol>
<li>蛇的运动其实就是蛇在进行body数组的更新，比如我们向右运动，本质就是body数组中，尾部删除，头部增加。所以蛇会渲染新的状态</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 蛇的运动</span></span><br><span class="line"><span class="title class_">Snake</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">update</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">pop</span>()</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">unshift</span>(&#123; <span class="string">&quot;row&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span>, <span class="string">&quot;col&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> + <span class="number">1</span> &#125;)</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ol>
<li>此时你会发现，蛇会边长而不是运动</li>
<li>这样是因为我们只是告诉了蛇如果渲染颜色， 之前渲染过的，需要擦除一下</li>
<li>此时我们给Game设置一个擦除屏幕的方法</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Game</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">clear</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 遍历表格，擦除画布</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="variable language_">this</span>.<span class="property">row</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="variable language_">this</span>.<span class="property">col</span>; j++) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">dom</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;tr&quot;</span>)[i].<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;td&quot;</span>)[j].<span class="property">style</span>.<span class="property">background</span> = <span class="string">&#x27;white&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ol>
<li>接下来我们要在定时器中进行游戏的三步走：清屏，更新和渲染</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> timer = <span class="built_in">setInterval</span>(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 定时器里面的核心就是游戏的渲染本质，清屏-更新-渲染</span></span><br><span class="line">    <span class="comment">// 清除屏幕</span></span><br><span class="line">    game.<span class="title function_">clear</span>();</span><br><span class="line">    <span class="comment">// 蛇的更新</span></span><br><span class="line">    game.<span class="property">snake</span>.<span class="title function_">update</span>();</span><br><span class="line">    <span class="comment">// 蛇的渲染</span></span><br><span class="line">    game.<span class="property">snake</span>.<span class="title function_">render</span>();</span><br><span class="line"></span><br><span class="line">&#125;, <span class="number">1000</span>)</span><br></pre></td></tr></table></figure>
<ol>
<li>此时的蛇就真正的运动起来了</li>
<li>接下来蛇要在不同的方向进行运动，此时我们可以给Game设置一个bindEvent事件，进行键盘的事件监听，改变运动方向</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 设置键盘的事件监听</span></span><br><span class="line"><span class="title class_">Game</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">bindEvent</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> self = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="comment">// 键盘事件</span></span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">onkeydown</span> = <span class="keyword">function</span>(<span class="params">event</span>) &#123;</span><br><span class="line">        <span class="keyword">switch</span> (event.<span class="property">keyCode</span>) &#123;</span><br><span class="line">            <span class="comment">// 按左键</span></span><br><span class="line">            <span class="keyword">case</span> <span class="number">37</span>:</span><br><span class="line">                self.<span class="property">snake</span>.<span class="property">direction</span> = <span class="string">&#x27;L&#x27;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">38</span>:</span><br><span class="line">                <span class="comment">// 按上键</span></span><br><span class="line">                self.<span class="property">snake</span>.<span class="property">direction</span> = <span class="string">&#x27;U&#x27;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">39</span>:</span><br><span class="line">                <span class="comment">// 按右键</span></span><br><span class="line">                self.<span class="property">snake</span>.<span class="property">direction</span> = <span class="string">&#x27;R&#x27;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">40</span>:</span><br><span class="line">                <span class="comment">// 按下键</span></span><br><span class="line">                self.<span class="property">snake</span>.<span class="property">direction</span> = <span class="string">&#x27;D&#x27;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ol>
<li>此时Snake类也必须有对应的方向匹配，我们在Snake的初始化的时候设置了一个this.direction = “R”</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Snake</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">update</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">switch</span> (<span class="variable language_">this</span>.<span class="property">direction</span>) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&quot;R&quot;</span>:</span><br><span class="line">            <span class="comment">// 向右移动</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">unshift</span>(&#123; <span class="string">&quot;row&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span>, <span class="string">&quot;col&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> + <span class="number">1</span> &#125;);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&quot;D&quot;</span>:</span><br><span class="line">            <span class="comment">// 向下移动</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">unshift</span>(&#123; <span class="string">&quot;row&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span> + <span class="number">1</span>, <span class="string">&quot;col&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> &#125;);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&quot;L&quot;</span>:</span><br><span class="line">            <span class="comment">// 向左移动</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">unshift</span>(&#123; <span class="string">&quot;row&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span>, <span class="string">&quot;col&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> - <span class="number">1</span> &#125;);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&quot;U&quot;</span>:</span><br><span class="line">            <span class="comment">// 向上移动</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">unshift</span>(&#123; <span class="string">&quot;row&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span> - <span class="number">1</span>, <span class="string">&quot;col&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> &#125;);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 蛇的不同方向的运动</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">pop</span>()</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ol>
<li>设置的方向变化的优化</li>
<li>此时我们发现蛇是可以原地调头的，不符合游戏的规则，所以我们需要优化</li>
<li>第一步我们在按键盘的时候需要先判定一下当前的蛇的运动方向，比如我们的蛇是朝右运动的，此时就不能按左</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 设置键盘的事件监听</span></span><br><span class="line"><span class="title class_">Game</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">bindEvent</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> self = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="comment">// 键盘事件</span></span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">onkeydown</span> = <span class="keyword">function</span>(<span class="params">event</span>) &#123;</span><br><span class="line">        <span class="keyword">switch</span> (event.<span class="property">keyCode</span>) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">case</span> <span class="number">37</span>:</span><br><span class="line">                <span class="comment">// 按左键</span></span><br><span class="line">                <span class="comment">// 先进行判断，如果当前的方向是向右移动，此时我们不能按左键</span></span><br><span class="line">                <span class="keyword">if</span> (self.<span class="property">snake</span>.<span class="property">direction</span> == <span class="string">&#x27;R&#x27;</span>) <span class="keyword">return</span>;</span><br><span class="line">                self.<span class="property">snake</span>.<span class="title function_">changeDirection</span>(<span class="string">&quot;L&quot;</span>)</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">38</span>:</span><br><span class="line">                <span class="comment">// 按上键</span></span><br><span class="line">                <span class="comment">// 先进行判断，如果当前的方向是向下移动，此时我们不能按上键</span></span><br><span class="line">                <span class="keyword">if</span> (self.<span class="property">snake</span>.<span class="property">direction</span> == <span class="string">&#x27;D&#x27;</span>) <span class="keyword">return</span>;</span><br><span class="line">                self.<span class="property">snake</span>.<span class="title function_">changeDirection</span>(<span class="string">&quot;U&quot;</span>)</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">39</span>:</span><br><span class="line">                <span class="comment">// 按右键</span></span><br><span class="line">                <span class="comment">// 先进行判断，如果当前的方向是向左移动，此时我们不能按右键</span></span><br><span class="line">                <span class="keyword">if</span> (self.<span class="property">snake</span>.<span class="property">direction</span> == <span class="string">&#x27;L&#x27;</span>) <span class="keyword">return</span>;</span><br><span class="line">                self.<span class="property">snake</span>.<span class="title function_">changeDirection</span>(<span class="string">&quot;R&quot;</span>)</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">40</span>:</span><br><span class="line">                <span class="comment">// 按下键</span></span><br><span class="line">                <span class="comment">// 先进行判断，如果当前的方向是向上移动，此时我们不能按下键</span></span><br><span class="line">                <span class="keyword">if</span> (self.<span class="property">snake</span>.<span class="property">direction</span> == <span class="string">&#x27;U&#x27;</span>) <span class="keyword">return</span>;</span><br><span class="line">                self.<span class="property">snake</span>.<span class="title function_">changeDirection</span>(<span class="string">&quot;D&quot;</span>)</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ol>
<li>第二步我们讲蛇的改变封装了一个方法，防止在每一帧中进行的方向冲突，比如我们的蛇虽然不能原地调头了，但是我们的渲染是在500ms后执行的，中间有500ms的空隙可以进行非常规操作，所以我们设置该方法解决这个问题</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 蛇的方向改变，防止的是在一次渲染之前会出现调头的情况</span></span><br><span class="line"><span class="title class_">Snake</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">changeDirection</span> = <span class="keyword">function</span>(<span class="params">d</span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">willDirection</span> = d;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol>
<li>蛇在下次update的时候，进行一次赋值就可以了</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Snake</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">update</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 当前的direction接收一下willDirection</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">direction</span> = <span class="variable language_">this</span>.<span class="property">willDirection</span>;</span><br><span class="line">    <span class="keyword">switch</span> (<span class="variable language_">this</span>.<span class="property">direction</span>) &#123;</span><br><span class="line">      .....</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 蛇的不同方向的运动</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">pop</span>()</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="蛇死亡的判断方式"><a href="#蛇死亡的判断方式" class="headerlink" title="蛇死亡的判断方式"></a>蛇死亡的判断方式</h3><ol>
<li>蛇有两种判断死亡的方式，第一种就是蛇自己超过了表格部分，第二种就是蛇自己头部和身体的某一个部位重合了</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 蛇的运动</span></span><br><span class="line"><span class="title class_">Snake</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">update</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 当前的direction接收一下willDirection</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">direction</span> = <span class="variable language_">this</span>.<span class="property">willDirection</span>;</span><br><span class="line">    <span class="keyword">switch</span> (<span class="variable language_">this</span>.<span class="property">direction</span>) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&quot;R&quot;</span>:</span><br><span class="line">            <span class="comment">// 向右移动</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">unshift</span>(&#123; <span class="string">&quot;row&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span>, <span class="string">&quot;col&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> + <span class="number">1</span> &#125;);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&quot;D&quot;</span>:</span><br><span class="line">            <span class="comment">// 向下移动</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">unshift</span>(&#123; <span class="string">&quot;row&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span> + <span class="number">1</span>, <span class="string">&quot;col&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> &#125;);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&quot;L&quot;</span>:</span><br><span class="line">            <span class="comment">// 向左移动</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">unshift</span>(&#123; <span class="string">&quot;row&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span>, <span class="string">&quot;col&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> - <span class="number">1</span> &#125;);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&quot;U&quot;</span>:</span><br><span class="line">            <span class="comment">// 向上移动</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">unshift</span>(&#123; <span class="string">&quot;row&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span> - <span class="number">1</span>, <span class="string">&quot;col&quot;</span>: <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> &#125;);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 死亡的判断，超出了的表格边缘的部分</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> &gt; game.<span class="property">col</span> - <span class="number">1</span> || <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span> &gt; game.<span class="property">row</span> - <span class="number">1</span> || <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> &lt; <span class="number">0</span> || <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span> &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="title function_">alert</span>(<span class="string">&quot;结束了&quot;</span>)</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">shift</span>() <span class="comment">//删除是因为当前的头增是不合法的，因为游戏已经结束了</span></span><br><span class="line">        <span class="built_in">clearInterval</span>(game.<span class="property">timer</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 自己撞到了自己的时候也会判定死亡</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">1</span>; i &lt; <span class="variable language_">this</span>.<span class="property">body</span>.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        <span class="comment">// 如果能够判断死亡，也就是当前的蛇的头部和身体的某一个部分row和col完全重合的时候</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> == <span class="variable language_">this</span>.<span class="property">body</span>[i].<span class="property">col</span> &amp;&amp; <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span> == <span class="variable language_">this</span>.<span class="property">body</span>[i].<span class="property">row</span>) &#123;</span><br><span class="line">            <span class="title function_">alert</span>(<span class="string">&quot;游戏结束&quot;</span>)</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">shift</span>() <span class="comment">//删除是因为当前的头增是不合法的，因为游戏已经结束了</span></span><br><span class="line">            <span class="built_in">clearInterval</span>(game.<span class="property">timer</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 蛇的不同方向的运动</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">pop</span>()</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="食物的创建"><a href="#食物的创建" class="headerlink" title="食物的创建"></a>食物的创建</h3><ol>
<li>此时我们创建一个Food类，用来产生食物</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Food</span>(<span class="params">game</span>) &#123;</span><br><span class="line">    <span class="comment">// 食物的位置</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">row</span> = <span class="built_in">parseInt</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * game.<span class="property">row</span>);</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">col</span> = <span class="built_in">parseInt</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * game.<span class="property">col</span>);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">row</span>, <span class="variable language_">this</span>.<span class="property">col</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title class_">Food</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">render</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    game.<span class="title function_">setHTML</span>(<span class="variable language_">this</span>.<span class="property">row</span>, <span class="variable language_">this</span>.<span class="property">col</span>, <span class="string">&quot;❤&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol>
<li>在Game进行实例化然后在定时器中进行渲染</li>
<li>但是此时我们会发现食物会渲染到蛇的身上</li>
<li>所以此时我们在随机食物的row和col的时候要先判断一下是否在蛇的身上</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Food</span>(<span class="params">gameSnake</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> self = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="comment">// 食物的位置</span></span><br><span class="line">    <span class="comment">// 下面的do-while循环语句作用是先创建一个row和col然后判断这个row和col是否在蛇的身上</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">do</span> &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">row</span> = <span class="built_in">parseInt</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * gameSnake.<span class="property">row</span>);</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">col</span> = <span class="built_in">parseInt</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * gameSnake.<span class="property">col</span>);</span><br><span class="line">    &#125; <span class="keyword">while</span> ((<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="comment">// 遍历蛇的row和col然后和Foo新随机出来的row和从来进行判断，是否重合</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; gameSnake.<span class="property">snake</span>.<span class="property">body</span>.<span class="property">length</span>; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (gameSnake.<span class="property">snake</span>.<span class="property">body</span>[i].<span class="property">row</span> == self.<span class="property">row</span> &amp;&amp; gameSnake.<span class="property">snake</span>.<span class="property">body</span>[i].<span class="property">col</span> == self.<span class="property">col</span>) &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;)());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="蛇吃食物"><a href="#蛇吃食物" class="headerlink" title="蛇吃食物"></a>蛇吃食物</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 蛇吃食物</span></span><br><span class="line"><span class="comment">// 判断如果当前的蛇的头部没有和食物进行重合，就代表此时没有吃到食物，此时就进行尾部删除，如果重合了就代表吃到了，此时我们不进行尾部删除。</span></span><br><span class="line"><span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">row</span> == game.<span class="property">food</span>.<span class="property">row</span> &amp;&amp; <span class="variable language_">this</span>.<span class="property">body</span>[<span class="number">0</span>].<span class="property">col</span> == game.<span class="property">food</span>.<span class="property">col</span>) &#123;</span><br><span class="line">    <span class="comment">// 此时情况是只有头部增加了，尾部没有删除</span></span><br><span class="line">    <span class="comment">// 创建新的食物</span></span><br><span class="line">    game.<span class="property">food</span> = <span class="keyword">new</span> <span class="title class_">Food</span>(game);</span><br><span class="line">    <span class="comment">// 加分数</span></span><br><span class="line">    game.<span class="property">score</span>++;</span><br><span class="line">    <span class="comment">// 让帧编号归0，因为蛇会蹿一下</span></span><br><span class="line">    game.<span class="property">f</span> = <span class="number">0</span>;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">body</span>.<span class="title function_">pop</span>()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="俄罗斯方块游戏"><a href="#俄罗斯方块游戏" class="headerlink" title="俄罗斯方块游戏"></a>俄罗斯方块游戏</h2><h3 id="初始化布局"><a href="#初始化布局" class="headerlink" title="初始化布局"></a>初始化布局</h3><ol>
<li>我们还是设置Game为中介者类</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">Game</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="comment">// 设置行和列数</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">row</span> = <span class="number">20</span>;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">col</span> = <span class="number">12</span>;</span><br><span class="line">        <span class="comment">// 初始化</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">init</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title class_">Game</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">init</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> $table = $(<span class="string">&quot;&lt;table&gt;&lt;/table&gt;&quot;</span>);</span><br><span class="line">        <span class="comment">// 渲染 表格</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="variable language_">this</span>.<span class="property">row</span>; i++) &#123;</span><br><span class="line">            <span class="comment">// 创建tr</span></span><br><span class="line">            <span class="keyword">var</span> $tr = $(<span class="string">&quot;&lt;tr&gt;&lt;/tr&gt;&quot;</span>)</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="variable language_">this</span>.<span class="property">col</span>; j++) &#123;</span><br><span class="line">                <span class="comment">// 创建td</span></span><br><span class="line">                <span class="keyword">var</span> $td = $(<span class="string">&quot;&lt;td&gt;&lt;/td&gt;&quot;</span>)</span><br><span class="line">                $td.<span class="title function_">appendTo</span>($tr)</span><br><span class="line">            &#125;</span><br><span class="line">            $tr.<span class="title function_">appendTo</span>($table)</span><br><span class="line">        &#125;</span><br><span class="line">        $($table).<span class="title function_">appendTo</span>(<span class="string">&quot;body&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>
<ol>
<li>你会发现我们的Game.js文件的最外层是用IIFE包裹起来的，和之前的贪吃蛇不同</li>
<li>因为我们需要知道实际工作中都是多个类进行并行开发的，由于为了上生产，会将代码进行打包部署，也就是将所有的js文件打包为一个js文件</li>
<li>如果没有IIFE会造成作用域和命名的冲突，所以我们用IIFE隔离作用域</li>
<li>CSS样式</li>
</ol>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">* &#123;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">0</span>;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">html</span> &#123;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">body</span> &#123;</span><br><span class="line">    <span class="attribute">overflow</span>: hidden;</span><br><span class="line">    <span class="attribute">background</span>: <span class="built_in">url</span>(<span class="string">images/bg2.png</span>) repeat-x center bottom, <span class="built_in">-webkit-linear-gradient</span>(top, skyblue, white);</span><br><span class="line">    <span class="attribute">background-size</span>: <span class="number">20%</span> auto;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">table</span> &#123;</span><br><span class="line">    <span class="attribute">border-collapse</span>: collapse;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">10px</span> auto;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">td</span> &#123;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#fff</span>;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">25px</span>;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">25px</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="认识方块"><a href="#认识方块" class="headerlink" title="认识方块"></a>认识方块</h3><ol>
<li>认识方块的状态</li>
<li>俄罗斯方块中，下落的方块形态一共有7种，每一种又有不同的状态（方向）</li>
<li>S型、Z型、J型、L型、O型、T型、I型</li>
<li>这些形态都用一个4*4的矩阵表示</li>
</ol>
<h3 id="方块的表示"><a href="#方块的表示" class="headerlink" title="方块的表示"></a>方块的表示</h3><ol>
<li>我们使用二维数组去表示俄罗斯方块的方块状态</li>
<li>比如：T我们使用二维数组</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[</span><br><span class="line">[<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>],</span><br><span class="line">[<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">0</span>],</span><br><span class="line">[<span class="number">1</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>],</span><br><span class="line">[<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>],</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<ol>
<li>我们会将所有的俄罗斯方块的类型和状态都放到一个JSON中，JSON是一个三维数组</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> fangkuai = &#123;</span><br><span class="line">    <span class="string">&quot;S&quot;</span>: [</span><br><span class="line">        [</span><br><span class="line">            [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]</span><br><span class="line">        ],</span><br><span class="line">        [</span><br><span class="line">            [<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]</span><br><span class="line">        ]</span><br><span class="line">],</span><br><span class="line">.....</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="渲染方块"><a href="#渲染方块" class="headerlink" title="渲染方块"></a>渲染方块</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">Block</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">block</span> = [</span><br><span class="line">            [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]</span><br><span class="line">        ];</span><br><span class="line">        <span class="comment">// 初始的行</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">row</span> = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">// 初始的列，因为要居中显示，所以列要为4</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">col</span> = <span class="number">4</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">render</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="comment">// 渲染四行四列的方块</span></span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">                <span class="comment">// 如果四乘四的矩阵中某一项不是0，就说明有颜色，渲染这个颜色</span></span><br><span class="line">                <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">block</span>[i][j] != <span class="number">0</span>) &#123;</span><br><span class="line">                    game.<span class="title function_">setColor</span>(i + <span class="variable language_">this</span>.<span class="property">row</span>, j + <span class="variable language_">this</span>.<span class="property">col</span>, <span class="variable language_">this</span>.<span class="property">block</span>[i][j])</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>
<ol>
<li>但是我们发现this.block是写死的，始终只有一种状态，所以接下来我们要随机显示方块</li>
<li>如何随机显示方块？我们一共有7种类型，每一种类型有不同的状态， 所以我们要把所有的类型和所有的状态都随机</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">Block</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="comment">// 得到随机的方块</span></span><br><span class="line">        <span class="comment">// 第一步罗列所有的类型</span></span><br><span class="line">        <span class="keyword">var</span> allType = [<span class="string">&quot;S&quot;</span>, <span class="string">&quot;T&quot;</span>, <span class="string">&quot;O&quot;</span>, <span class="string">&quot;L&quot;</span>, <span class="string">&quot;J&quot;</span>, <span class="string">&quot;I&quot;</span>, <span class="string">&quot;Z&quot;</span>];</span><br><span class="line">        <span class="comment">// 第二步从所有的类型中随机得到一种</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">type</span> = allType[<span class="built_in">parseInt</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * allType.<span class="property">length</span>)];</span><br><span class="line">        <span class="comment">// 第三步得到随机的类型方块，然后通过这个类型获取当前的类型所有形状总数量，因为不同的类型，形状数量不同，比如O只有一种I有两种，L有4种</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">allDir</span> = fangkuai[<span class="variable language_">this</span>.<span class="property">type</span>].<span class="property">length</span>;</span><br><span class="line">        <span class="comment">// 第四步通过当前的allDir的length随机得到不同的数字</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">dir</span> = <span class="built_in">parseInt</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="variable language_">this</span>.<span class="property">allDir</span>);</span><br><span class="line">        <span class="comment">// 第五步得到随机的方块</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">code</span> = fangkuai[<span class="variable language_">this</span>.<span class="property">type</span>][<span class="variable language_">this</span>.<span class="property">dir</span>]</span><br><span class="line">            <span class="comment">// 初始的行</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">row</span> = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">// 初始的列，因为要居中显示，所以列要为4</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">col</span> = <span class="number">4</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">render</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="comment">// 渲染四行四列的方块</span></span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">                <span class="comment">// 如果四乘四的矩阵中某一项不是0，就说明有颜色，渲染这个颜色</span></span><br><span class="line">                <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">code</span>[i][j] != <span class="number">0</span>) &#123;</span><br><span class="line">                    game.<span class="title function_">setColor</span>(i + <span class="variable language_">this</span>.<span class="property">row</span>, j + <span class="variable language_">this</span>.<span class="property">col</span>, <span class="variable language_">this</span>.<span class="property">code</span>[i][j])</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>
<h3 id="地图类"><a href="#地图类" class="headerlink" title="地图类"></a>地图类</h3><ol>
<li>我们需要一个地图，来显示已经到底的方块，此时我们的表格只是一个显示的dom，没有办法实时进行显示，因为每一帧要清除画布，所以要想持久，就需要让数据持久起来，所以我们维持一个地图类，用来持久已经到底的方块</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">Map</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="comment">// 地图的矩阵</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">mapCode</span> = [</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">2</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">6</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>],</span><br><span class="line">            [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title class_">Map</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">render</span> = <span class="keyword">function</span>(<span class="params">mapGame</span>) &#123;</span><br><span class="line">        <span class="comment">// 渲染地图</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; mapGame.<span class="property">row</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; mapGame.<span class="property">col</span>; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">mapCode</span>[i][j] != <span class="number">0</span>) &#123;</span><br><span class="line">                    game.<span class="title function_">setColor</span>(i, j, <span class="variable language_">this</span>.<span class="property">mapCode</span>[i][j])</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>
<h3 id="方块的下落状态"><a href="#方块的下落状态" class="headerlink" title="方块的下落状态"></a>方块的下落状态</h3><ol>
<li>此时我们的方块下落很简单，就是给Block.row++就可以了，但是怎么停止？</li>
<li>此时我们提出了一个“预判断”的概念，也就是每一次方块下落的时候需要进行一次预先判断，当前方块的下一次运动位置去和地图的（mapCode）进行位置判断，如果此时mapCode在相同的位置也有方块存在，则停止下落。</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">check</span> = <span class="keyword">function</span>(<span class="params">row, col</span>) &#123;</span><br><span class="line">    <span class="comment">// check函数的row和col指的是要校验的地图的row和col的位置</span></span><br><span class="line">    <span class="comment">// 能力判断方法，判读的是对应位置的方块和地图是否有都不为0的情况，如果有返回true，否则返回false；false就代表没有重合</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">code</span>[i][j] != <span class="number">0</span> &amp;&amp; game.<span class="property">map</span>.<span class="property">mapCode</span>[i + row][j + col] !== <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 方块下落，需要判断当前的方块是否能够下落</span></span><br><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkDown</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 判断当前地图的位置和自己方块的位置是否有重合this.row+1指的是预判断</span></span><br><span class="line">    <span class="comment">// 与判断就是在下一次方块将要到达的位置是否有对应的地图不为0</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="title function_">check</span>(<span class="variable language_">this</span>.<span class="property">row</span> + <span class="number">1</span>, <span class="variable language_">this</span>.<span class="property">col</span>)) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">row</span>++</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 此时就是下落到底的状态，渲染新的方块</span></span><br><span class="line">        game.<span class="property">block</span> = <span class="keyword">new</span> <span class="title class_">Block</span>()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="渲染地图"><a href="#渲染地图" class="headerlink" title="渲染地图"></a>渲染地图</h3><ol>
<li>当前我们的方块到底之后，不会渲染当前的状态，只会消失，因为是mapCode一直在维护底部的持久状态，所以一旦方块到底后，就要将数据给mapCode进行持久更新</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 方块下落，需要判断当前的方块是否能够下落</span></span><br><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkDown</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 判断当前地图的位置和自己方块的位置是否有重合this.row+1指的是预判断</span></span><br><span class="line">    <span class="comment">// 与判断就是在下一次方块将要到达的位置是否有对应的地图不为0</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="title function_">check</span>(<span class="variable language_">this</span>.<span class="property">row</span> + <span class="number">1</span>, <span class="variable language_">this</span>.<span class="property">col</span>)) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">row</span>++</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 此时就是下落到底的状态，渲染新的方块</span></span><br><span class="line">        game.<span class="property">block</span> = <span class="keyword">new</span> <span class="title class_">Block</span>();</span><br><span class="line">        <span class="comment">// 方块到底了，然后要渲染到地图的code中</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">renderMap</span>()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将已经到底的方块渲染到地图中</span></span><br><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">renderMap</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">            <span class="comment">// 将现在已有的方块渲染到Map类的mapCode上</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">code</span>[i][j] !== <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="comment">// 改变地图的mapCode数据</span></span><br><span class="line">                game.<span class="property">map</span>.<span class="property">mapCode</span>[<span class="variable language_">this</span>.<span class="property">row</span> + i][<span class="variable language_">this</span>.<span class="property">col</span> + j] = <span class="variable language_">this</span>.<span class="property">code</span>[i][j]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="方块的左右移动"><a href="#方块的左右移动" class="headerlink" title="方块的左右移动"></a>方块的左右移动</h3><ol>
<li>其实就是增加事件监听，然后通过check方法预判断是否有能力移动</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Game</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">bindEvent</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 备份</span></span><br><span class="line">    <span class="keyword">var</span> self = <span class="variable language_">this</span>;</span><br><span class="line">    $(<span class="variable language_">document</span>).<span class="title function_">keydown</span>(<span class="keyword">function</span>(<span class="params">event</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (event.<span class="property">keyCode</span> == <span class="number">37</span>) &#123;</span><br><span class="line">            <span class="comment">// 判断时候有向左移动的能力</span></span><br><span class="line">            self.<span class="property">block</span>.<span class="title function_">checkLeft</span>()</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (event.<span class="property">keyCode</span> == <span class="number">39</span>) &#123;</span><br><span class="line">            <span class="comment">// 判断时候有向右移动的能力</span></span><br><span class="line">            self.<span class="property">block</span>.<span class="title function_">checkRight</span>()</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (event.<span class="property">keyCode</span> == <span class="number">32</span>) &#123;</span><br><span class="line">            <span class="comment">// 一键到底，空格到底</span></span><br><span class="line">            self.<span class="property">block</span>.<span class="title function_">checkBlockEnd</span>()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 判断是否能够向左移动，如果可以则移动</span></span><br><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkLeft</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 判断是否可以向左</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="title function_">check</span>(<span class="variable language_">this</span>.<span class="property">row</span>, <span class="variable language_">this</span>.<span class="property">col</span> - <span class="number">1</span>)) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">col</span>--;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 判断是否能够向右移动，如果可以则移动</span></span><br><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkRight</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 判断是否可以向左</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="title function_">check</span>(<span class="variable language_">this</span>.<span class="property">row</span>, <span class="variable language_">this</span>.<span class="property">col</span> + <span class="number">1</span>)) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">col</span>++;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkBlockEnd</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 使用while循环，如果当前的check返回的是true则代表能够下移，继续让row++</span></span><br><span class="line">    <span class="keyword">while</span> (<span class="variable language_">this</span>.<span class="title function_">check</span>(<span class="variable language_">this</span>.<span class="property">row</span> + <span class="number">1</span>, <span class="variable language_">this</span>.<span class="property">col</span>)) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">row</span>++</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="方块的旋转"><a href="#方块的旋转" class="headerlink" title="方块的旋转"></a>方块的旋转</h3><ol>
<li>我们可以按上键让方块按照顺时针的方向旋转</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 方块的旋转</span></span><br><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkRot</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">dir</span>++;</span><br><span class="line">    <span class="comment">// 判断如果当前dir大于了最后一种方向，回到第1种状态</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">dir</span> &gt; <span class="variable language_">this</span>.<span class="property">allDir</span> - <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">dir</span> = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 改变方向之后渲染新的方块方向</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">code</span> = fangkuai[<span class="variable language_">this</span>.<span class="property">type</span>][<span class="variable language_">this</span>.<span class="property">dir</span>];</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ol>
<li>但是此时会发生一个问题，方块在旋转的过程中不会顾及左右是否有已有的颜色格子</li>
<li>此时我们可以对方块进行一次“备份”，将旧的方块备份一份，然后让新block的和已有的mapCode进行一一比对，如果新的有重合就打回原形</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkRot</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 备份旧的形状方向</span></span><br><span class="line">    <span class="keyword">var</span> oldDir = <span class="variable language_">this</span>.<span class="property">dir</span>;</span><br><span class="line">    <span class="comment">// 改变新的</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">dir</span>++;</span><br><span class="line">    <span class="comment">// 判断如果当前dir大于了最后一种方向，回到第1种状态</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">dir</span> &gt; <span class="variable language_">this</span>.<span class="property">allDir</span> - <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">dir</span> = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 改变方向之后渲染新的方块方向</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">code</span> = fangkuai[<span class="variable language_">this</span>.<span class="property">type</span>][<span class="variable language_">this</span>.<span class="property">dir</span>];</span><br><span class="line">    <span class="comment">// 渲染之后的新方块需要判断，是否有能力进行渲染</span></span><br><span class="line">    <span class="keyword">if</span> (!<span class="variable language_">this</span>.<span class="title function_">check</span>(<span class="variable language_">this</span>.<span class="property">row</span>, <span class="variable language_">this</span>.<span class="property">col</span>)) &#123;</span><br><span class="line">        <span class="comment">// 进入这里了就说明重合了，违规了，打回原形</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">dir</span> = oldDir;</span><br><span class="line">        <span class="comment">// 再次渲染方块</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">code</span> = fangkuai[<span class="variable language_">this</span>.<span class="property">type</span>][<span class="variable language_">this</span>.<span class="property">dir</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="方块消行"><a href="#方块消行" class="headerlink" title="方块消行"></a>方块消行</h3><ol>
<li>方块消行本质就是mapCode的每一项数组的遍历，如果某一项数组中有都不为0了，就说明该消行了</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Map</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkRemove</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">//判断当前的mapCode是否该消行</span></span><br><span class="line">    <span class="comment">// 消行规则：当前的mapCode数组的每一项如果都不是0了，就说明该消行了</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">20</span>; i++) &#123;</span><br><span class="line">        <span class="comment">// 遍历地图数组进行判断</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">mapCode</span>[i].<span class="title function_">indexOf</span>(<span class="number">0</span>) == -<span class="number">1</span>) &#123;</span><br><span class="line">            <span class="comment">// 删除这一行</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">mapCode</span>.<span class="title function_">splice</span>(i, <span class="number">1</span>)</span><br><span class="line">             <span class="comment">// 删除一行补充一行</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">mapCode</span>.<span class="title function_">unshift</span>([<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>])</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="判断游戏结束"><a href="#判断游戏结束" class="headerlink" title="判断游戏结束"></a>判断游戏结束</h3><ol>
<li>地图的第一行的mapCode有不为0的了就判定死亡了</li>
<li>每次方块到底的时候进行一次判断</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkOver</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; game.<span class="property">col</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (game.<span class="property">map</span>.<span class="property">mapCode</span>[<span class="number">0</span>][i] != <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">clearInterval</span>(game.<span class="property">timer</span>);</span><br><span class="line">            <span class="title function_">alert</span>(<span class="string">&quot;游戏结束&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="设置预览框"><a href="#设置预览框" class="headerlink" title="设置预览框"></a>设置预览框</h3><ol>
<li>俄罗斯方块有一个重要的逻辑就是预览，下一次的方块 会 提前展示，所以我们可以设置一个nextBlock当做下一次出场的方块</li>
<li>渲染预览框的方法</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Game</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">setNextColor</span> = <span class="keyword">function</span>(<span class="params">row, col, num</span>) &#123;</span><br><span class="line">    <span class="comment">// 给对应的有颜色的方块添加类名</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">nextBlock</span>.<span class="property">code</span>[i][j] != <span class="number">0</span>) &#123;</span><br><span class="line">                $(<span class="string">&quot;.tab2&quot;</span>).<span class="title function_">find</span>(<span class="string">&quot;tr&quot;</span>).<span class="title function_">eq</span>(i).<span class="title function_">children</span>(<span class="string">&quot;td&quot;</span>).<span class="title function_">eq</span>(j).<span class="title function_">addClass</span>(<span class="string">&quot;c&quot;</span> + <span class="variable language_">this</span>.<span class="property">nextBlock</span>.<span class="property">code</span>[i][j])</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ol>
<li>初始化的时候增加预览框的功能</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Game</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">init</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 初始化大表格</span></span><br><span class="line">    <span class="keyword">var</span> $table = $(<span class="string">&quot;&lt;table&gt;&lt;/table&gt;&quot;</span>);</span><br><span class="line">    $table.<span class="title function_">addClass</span>(<span class="string">&#x27;tab1&#x27;</span>);</span><br><span class="line">    <span class="comment">// 渲染 表格</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="variable language_">this</span>.<span class="property">row</span>; i++) &#123;</span><br><span class="line">        <span class="comment">// 创建tr</span></span><br><span class="line">        <span class="keyword">var</span> $tr = $(<span class="string">&quot;&lt;tr&gt;&lt;/tr&gt;&quot;</span>)</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="variable language_">this</span>.<span class="property">col</span>; j++) &#123;</span><br><span class="line">            <span class="comment">// 创建td</span></span><br><span class="line">            <span class="keyword">var</span> $td = $(<span class="string">&quot;&lt;td&gt;&lt;/td&gt;&quot;</span>)</span><br><span class="line">            $td.<span class="title function_">appendTo</span>($tr)</span><br><span class="line">        &#125;</span><br><span class="line">        $tr.<span class="title function_">appendTo</span>($table)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 初始化预览窗口</span></span><br><span class="line">    <span class="keyword">var</span> $table2 = $(<span class="string">&quot;&lt;table&gt;&lt;/table&gt;&quot;</span>);</span><br><span class="line">    $table2.<span class="title function_">addClass</span>(<span class="string">&#x27;tab2&#x27;</span>);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">        <span class="comment">// 创建tr</span></span><br><span class="line">        <span class="keyword">var</span> $tr2 = $(<span class="string">&quot;&lt;tr&gt;&lt;/tr&gt;&quot;</span>)</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">            <span class="comment">// 创建td</span></span><br><span class="line">            <span class="keyword">var</span> $td2 = $(<span class="string">&quot;&lt;td&gt;&lt;/td&gt;&quot;</span>)</span><br><span class="line">            $td2.<span class="title function_">appendTo</span>($tr2)</span><br><span class="line">        &#125;</span><br><span class="line">        $tr2.<span class="title function_">appendTo</span>($table2)</span><br><span class="line">    &#125;</span><br><span class="line">    $($table).<span class="title function_">appendTo</span>(<span class="string">&quot;body&quot;</span>)</span><br><span class="line">    $($table2).<span class="title function_">appendTo</span>(<span class="string">&quot;body&quot;</span>)</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol>
<li>当方块到底的时候，渲染nextBlock状态</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Block</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">checkDown</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 判断当前地图的位置和自己方块的位置是否有重合this.row+1指的是预判断</span></span><br><span class="line">    <span class="comment">// 与判断就是在下一次方块将要到达的位置是否有对应的地图不为0</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="title function_">check</span>(<span class="variable language_">this</span>.<span class="property">row</span> + <span class="number">1</span>, <span class="variable language_">this</span>.<span class="property">col</span>)) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">row</span>++</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 此时就是下落到底的状态，渲染预览框的方块</span></span><br><span class="line">        game.<span class="property">block</span> = game.<span class="property">nextBlock</span>;</span><br><span class="line">        <span class="comment">// 让预览框的方块再次渲染新的</span></span><br><span class="line">        game.<span class="property">nextBlock</span> = <span class="keyword">new</span> <span class="title class_">Block</span>();</span><br><span class="line">        <span class="comment">// 方块到底了，然后要渲染到地图的code中</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">renderMap</span>();</span><br><span class="line">        <span class="comment">// 判断是否可以消行</span></span><br><span class="line">        game.<span class="property">map</span>.<span class="title function_">checkRemove</span>();</span><br><span class="line">        <span class="comment">// 进行判断是否游戏结束</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">checkOver</span>()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://example.com">晚夜微雨问海棠</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/posts/255e6169.html">http://example.com/posts/255e6169.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">晚夜微雨问海棠のBlog</a>！</span></div></div><div class="tag_share"><div class="post_share"><div class="social-share" data-image="https://s1.ax1x.com/2022/11/06/xXrJCF.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://s1.ax1x.com/2022/11/15/zE9P7d.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="https://s1.ax1x.com/2022/11/15/zE9P7d.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://s1.ax1x.com/2022/11/15/zE9ung.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="https://s1.ax1x.com/2022/11/15/zE9ung.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%B8%B8%E6%88%8F"><span class="toc-number">1.</span> <span class="toc-text">游戏</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B4%AA%E5%90%83%E8%9B%87%E6%B8%B8%E6%88%8F"><span class="toc-number">1.1.</span> <span class="toc-text">贪吃蛇游戏</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9D%E5%A7%8BGame%E7%B1%BB%E7%BB%93%E6%9E%84"><span class="toc-number">1.1.1.</span> <span class="toc-text">初始Game类结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%B2%E6%9F%93%E8%9B%87%E7%9A%84%E9%A2%9C%E8%89%B2"><span class="toc-number">1.1.2.</span> <span class="toc-text">渲染蛇的颜色</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%9B%87%E7%9A%84%E8%BF%90%E5%8A%A8"><span class="toc-number">1.1.3.</span> <span class="toc-text">蛇的运动</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%9B%87%E6%AD%BB%E4%BA%A1%E7%9A%84%E5%88%A4%E6%96%AD%E6%96%B9%E5%BC%8F"><span class="toc-number">1.1.4.</span> <span class="toc-text">蛇死亡的判断方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A3%9F%E7%89%A9%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="toc-number">1.1.5.</span> <span class="toc-text">食物的创建</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%9B%87%E5%90%83%E9%A3%9F%E7%89%A9"><span class="toc-number">1.1.6.</span> <span class="toc-text">蛇吃食物</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97%E6%B8%B8%E6%88%8F"><span class="toc-number">1.2.</span> <span class="toc-text">俄罗斯方块游戏</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E5%B8%83%E5%B1%80"><span class="toc-number">1.2.1.</span> <span class="toc-text">初始化布局</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86%E6%96%B9%E5%9D%97"><span class="toc-number">1.2.2.</span> <span class="toc-text">认识方块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%9D%97%E7%9A%84%E8%A1%A8%E7%A4%BA"><span class="toc-number">1.2.3.</span> <span class="toc-text">方块的表示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%B2%E6%9F%93%E6%96%B9%E5%9D%97"><span class="toc-number">1.2.4.</span> <span class="toc-text">渲染方块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%B0%E5%9B%BE%E7%B1%BB"><span class="toc-number">1.2.5.</span> <span class="toc-text">地图类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%9D%97%E7%9A%84%E4%B8%8B%E8%90%BD%E7%8A%B6%E6%80%81"><span class="toc-number">1.2.6.</span> <span class="toc-text">方块的下落状态</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%B2%E6%9F%93%E5%9C%B0%E5%9B%BE"><span class="toc-number">1.2.7.</span> <span class="toc-text">渲染地图</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%9D%97%E7%9A%84%E5%B7%A6%E5%8F%B3%E7%A7%BB%E5%8A%A8"><span class="toc-number">1.2.8.</span> <span class="toc-text">方块的左右移动</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%9D%97%E7%9A%84%E6%97%8B%E8%BD%AC"><span class="toc-number">1.2.9.</span> <span class="toc-text">方块的旋转</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%9D%97%E6%B6%88%E8%A1%8C"><span class="toc-number">1.2.10.</span> <span class="toc-text">方块消行</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E6%B8%B8%E6%88%8F%E7%BB%93%E6%9D%9F"><span class="toc-number">1.2.11.</span> <span class="toc-text">判断游戏结束</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AE%E9%A2%84%E8%A7%88%E6%A1%86"><span class="toc-number">1.2.12.</span> <span class="toc-text">设置预览框</span></a></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('https://s1.ax1x.com/2022/11/06/xXrJCF.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By 晚夜微雨问海棠</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><div class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></div><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '',
      appKey: '',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><div class="aplayer no-destroy" data-id="7625372994" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true"> </div> <script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script> <script src="/swiper/swiper.min.js"></script> <script src="/swiper/swiperindex.js"></script> <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script> <script src="/js/title.js"></script> <script defer data-pjax src="/js/cat.js"></script> <script defer data-pjax src="/js/rightMenu.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show","#web_bg",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function history_calendar_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div>';
                console.log('已挂载history_calendar')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='/'|| '/' ==='all')){

            history_calendar_injector_config()
        } </script><script data-pjax  src="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.js"></script><script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><script data-pjax>
    function butterfly_categories_card_injector_config(){
      var parent_div_git = document.getElementById('recent-posts');
      var item_html = '<style>li.categoryBar-list-item{width:24%;}.categoryBar-list{max-height: 190px;overflow:auto;}.categoryBar-list::-webkit-scrollbar{width:0!important}@media screen and (max-width: 650px){.categoryBar-list{max-height: 160px;}}</style><div class="recent-post-item" style="height:auto;width:100%;padding:0px;"><div id="categoryBar"><ul class="categoryBar-list"><li class="categoryBar-list-item" style="background:url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpicnew6.photophoto.cn%2F20120106%2Fhaimianbaobaotupian-10036689_1.jpg&amp;refer=http%3A%2F%2Fpicnew6.photophoto.cn&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1665648815&amp;t=a3deb15ddd7a38597964c3a05e519bd7);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/Asp-Net/&quot;);" href="javascript:void(0);">Asp.Net</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://npm.elemecdn.com/anzhiyu-blog@1.1.6/img/post/common/site-backgound.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/C-系列/&quot;);" href="javascript:void(0);">C#系列</a><span class="categoryBar-list-count">8</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13012860773%2F1000.jpg&amp;refer=http%3A%2F%2Finews.gtimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1665648865&amp;t=1d2f13b52a7e279e8801545f59eda4e7);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/Hexo重装记录/&quot;);" href="javascript:void(0);">Hexo重装记录</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202011%2F07%2F20201107183300_ac0c5.thumb.1000_0.jpeg&amp;refer=http%3A%2F%2Fc-ssl.duitang.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1665648887&amp;t=3d32c6b12a4cf12e5cec3585b79c1fc3);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/EF/&quot;);" href="javascript:void(0);">EF</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2F25%2Feb%2F1f%2F25eb1fd40b5a740c2b79e33f700806e5.jpg&amp;refer=http%3A%2F%2Fup.enterdesk.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1665648908&amp;t=6425ee3bd166b0f5c5ffa6fb18b6937b);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/LOL/&quot;);" href="javascript:void(0);">LOL</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2Ftx26%2F36080017392073.jpg&amp;refer=http%3A%2F%2Flmg.jj20.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1665650136&amp;t=5cc414d0ee58c9e285218d92f6a95513);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/idea/&quot;);" href="javascript:void(0);">idea</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn10%2F320%2Fw700h420%2F20180907%2F5116-hitesuz6599636.jpg&amp;refer=http%3A%2F%2Fn.sinaimg.cn&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1665650004&amp;t=6a304aa9cc7435ec1d148b8f99092bc0);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/世界/&quot;);" href="javascript:void(0);">世界</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://s1.ax1x.com/2022/11/15/zAxCi6.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/世界见闻录/&quot;);" href="javascript:void(0);">世界见闻录</a><span class="categoryBar-list-count">8</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://s1.ax1x.com/2022/11/15/zAxFzD.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/前端体系/&quot;);" href="javascript:void(0);">前端体系</a><span class="categoryBar-list-count">19</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://s1.ax1x.com/2022/11/15/zAxnot.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/副业/&quot;);" href="javascript:void(0);">副业</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://s1.ax1x.com/2022/11/15/zAxMJf.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/文案/&quot;);" href="javascript:void(0);">文案</a><span class="categoryBar-list-count">6</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://s1.ax1x.com/2022/11/15/zAxQW8.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/办公日常/&quot;);" href="javascript:void(0);">办公日常</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://s1.ax1x.com/2022/11/15/zAx3Qg.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/朝花夕拾/&quot;);" href="javascript:void(0);">朝花夕拾</a><span class="categoryBar-list-count">12</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(https://s1.ax1x.com/2022/11/15/zAxYes.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/前端/&quot;);" href="javascript:void(0);">前端</a><span class="categoryBar-list-count">20</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(undefined);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/杂七杂八/&quot;);" href="javascript:void(0);">杂七杂八</a><span class="categoryBar-list-count">7</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(undefined);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/简书/&quot;);" href="javascript:void(0);">简书</a><span class="categoryBar-list-count">5</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(undefined);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/游戏/&quot;);" href="javascript:void(0);">游戏</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li></ul></div></div>';
      console.log('已挂载butterfly_categories_card')
      parent_div_git.insertAdjacentHTML("afterbegin",item_html)
      }
    if( document.getElementById('recent-posts') && (location.pathname ==='/'|| '/' ==='all')){
    butterfly_categories_card_injector_config()
    }
  </script><script data-pjax>
  function butterfly_clock_anzhiyu_injector_config(){
    var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
    var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img class="entered loading" id="card-clock-loading" src= "" data-lazy-src="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading"/></div></div></div></div></div>';
    console.log('已挂载butterfly_clock_anzhiyu')
    parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  var elist = 'null'.split(',');
  var cpage = location.pathname;
  var epage = '/';
  var qweather_key = '41ea442da35546559913a6a5d54163b2';
  var gaud_map_key = '6b62fbebf584b9a22a85bfccbf90335e';
  var baidu_ak_key = 'undefined';
  var flag = 0;
  var clock_rectangle = '114.35,34.79';
  var clock_default_rectangle_enable = 'true';

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_clock_anzhiyu_injector_config();
  }
  else if (epage === cpage){
    butterfly_clock_anzhiyu_injector_config();
  }
  </script><script src="https://widget.qweather.net/simple/static/js/he-simple-common.js?v=2.0"></script><script data-pjax src="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.js"></script><script data-pjax>
  function butterfly_footer_beautify_injector_config(){
    var parent_div_git = document.getElementById('footer-wrap');
    var item_html = '<div id="workboard"></div><p id="ghbdages"><a class="github-badge" target="_blank" href="https://hexo.io/" style="margin-inline:5px" data-title="博客框架为Hexo_v5.4.0" title=""><img src= "" data-lazy-src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&amp;logo=hexo" alt=""/></a><a class="github-badge" target="_blank" href="https://butterfly.js.org/" style="margin-inline:5px" data-title="主题版本Butterfly_v4.2.2" title=""><img src= "" data-lazy-src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&amp;logo=bitdefender" alt=""/></a><a class="github-badge" target="_blank" href="https://www.jsdelivr.com/" style="margin-inline:5px" data-title="本站使用JsDelivr为静态资源提供CDN加速" title=""><img src= "" data-lazy-src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&amp;logo=jsDelivr" alt=""/></a><a class="github-badge" target="_blank" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" style="margin-inline:5px" data-title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可" title=""><img src= "" data-lazy-src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&amp;logo=Claris" alt=""/></a></p>';
    console.log('已挂载butterfly_footer_beautify')
    parent_div_git.insertAdjacentHTML("beforeend",item_html)
    }
  var elist = 'null'.split(',');
  var cpage = location.pathname;
  var epage = 'all';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_footer_beautify_injector_config();
  }
  else if (epage === cpage){
    butterfly_footer_beautify_injector_config();
  }
  </script><script async src="/js/runtime/runtime.min.js"></script><script data-pjax>
  function butterfly_swiper_injector_config(){
    var parent_div_git = document.getElementById('recent-posts');
    var item_html = '<div class="recent-post-item swiper_container_card" style="height: auto;width: 100%"><div id="random"><div id="random-banner"><canvas id="peoplecanvas"></canvas></div><a id="random-hover" style="width:100%;height:auto;" href="javascript:toRandomPost()" rel="external nofollow noreferrer" one-link-mark="yes"><i class="fa fa-paper-plane" style="margin-left:10px"></i><div style="margin-left:10px">随便逛逛<i class="fa-solid fa-arrow-right" style="margin-left:10px"></i></div></a></div><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/67fcd28d.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://img2.baidu.com/it/u=4087346614,241521689&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=500&amp;h=333" alt="" onerror="this.src=https://cdn.cbd.int/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2022-09-19</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/67fcd28d.html&quot;);" href="javascript:void(0);" alt="">学生人到职业人_心态篇</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/67fcd28d.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/8a73f76c.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://s1.ax1x.com/2022/11/04/xLiBTA.jpg" alt="" onerror="this.src=https://cdn.cbd.int/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2022-11-04</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/8a73f76c.html&quot;);" href="javascript:void(0);" alt="">生活の奇思妙想☞2022</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/8a73f76c.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/14e5dae6.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://s1.ax1x.com/2022/10/21/xcAQDH.jpg" alt="" onerror="this.src=https://cdn.cbd.int/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2022-11-12</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/14e5dae6.html&quot;);" href="javascript:void(0);" alt="">少年自负凌云笔</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/14e5dae6.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/9b7092b7.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://s1.ax1x.com/2022/10/11/xNJbF0.jpg" alt="" onerror="this.src=https://cdn.cbd.int/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2022-09-13</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/9b7092b7.html&quot;);" href="javascript:void(0);" alt="">租房有感</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/9b7092b7.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/94bbc7d.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://s1.ax1x.com/2022/10/11/xNJLWT.jpg" alt="" onerror="this.src=https://cdn.cbd.int/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2022-09-25</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/94bbc7d.html&quot;);" href="javascript:void(0);" alt="">谷歌收藏</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/94bbc7d.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/3b350f14.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://s1.ax1x.com/2022/10/11/xNJJd1.jpg" alt="" onerror="this.src=https://cdn.cbd.int/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2022-09-13</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/3b350f14.html&quot;);" href="javascript:void(0);" alt="">阳光不燥、微风正好</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/3b350f14.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
    console.log('已挂载butterfly_swiper')
    parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  var elist = 'undefined'.split(',');
  var cpage = location.pathname;
  var epage = '/';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_swiper_injector_config();
  }
  else if (epage === cpage){
    butterfly_swiper_injector_config();
  }
  </script><script defer src="https://npm.elemecdn.com/anzhiyu-blog@1.1.6/js/swiper.min.js"></script><script defer data-pjax src="https://cdn.cbd.int/hexo-butterfly-swiper-anzhiyu@1.0.4/lib/swiper_init.js"></script><script data-pjax src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/gsap/3.9.1/gsap.min.js"></script><script defer src="https://npm.elemecdn.com/hexo-butterfly-swiper-anzhiyu@1.0.4/lib/people.min.js"></script><script async src="/anzhiyu/random.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1.5s');
    arr[i].setAttribute('data-wow-delay', '200ms');
    arr[i].setAttribute('data-wow-offset', '30');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '200ms');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script><script async="async">var arr = document.getElementsByClassName('flink-list-card');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__flipInY');
    arr[i].setAttribute('data-wow-duration', '3s');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script><script async="async">var arr = document.getElementsByClassName('flink-list-card');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__animated');
    arr[i].setAttribute('data-wow-duration', '3s');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script><script async="async">var arr = document.getElementsByClassName('article-sort-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__slideInRight');
    arr[i].setAttribute('data-wow-duration', '1.5s');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script><script async="async">var arr = document.getElementsByClassName('site-card');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__flipInY');
    arr[i].setAttribute('data-wow-duration', '3s');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script><script async="async">var arr = document.getElementsByClassName('site-card');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__animated');
    arr[i].setAttribute('data-wow-duration', '3s');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.cbd.int/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://cdn.cbd.int/hexo-butterfly-wowjs/lib/wow_init.js"></script><script async src="/js/ali_font.js"></script><!-- hexo injector body_end end --><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"left","width":150,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/"});</script></body></html>